package com.gemme.gemme.shequ;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager.widget.ViewPager;

import com.gemme.gemme.R;
import com.gemme.gemme.fragment.BaseLazyFragment;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;


/**
 * Created by sp on 2019/7/18.
 * 首页
 */
public class ShequFragment extends BaseLazyFragment {

    View inflate;
    Context mContext;
    FragmentActivity act;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private FmPagerAdapter pagerAdapter;
    private ArrayList<Fragment> fragments = new ArrayList<>();
    private String[] titles = new String[]{"关注", "广场"};

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        inflate = inflater.inflate(R.layout.fragment_shequ, container, false);
        mContext = ShequFragment.this.getContext();
        initView(inflate);

        return inflate;
    }

    private void initView(View inflate) {
        if (mContext instanceof FragmentActivity) {
            act = (FragmentActivity) mContext;
            tabLayout = (TabLayout) inflate.findViewById(R.id.tablayout);
            viewPager = (ViewPager) inflate.findViewById(R.id.viewpager);

            fragments.add(new Guanzhu2Fragment());
            fragments.add(new Guangchang2Fragment());

            tabLayout.addTab(tabLayout.newTab());
            tabLayout.addTab(tabLayout.newTab());

            tabLayout.setupWithViewPager(viewPager, false);
            pagerAdapter = new FmPagerAdapter(fragments, act.getSupportFragmentManager());
            viewPager.setAdapter(pagerAdapter);
            for (int i = 0; i < titles.length; i++) {
                tabLayout.getTabAt(i).setText(titles[i]);
            }
        }

        toGuangchang();
    }

    public void toGuangchang() {
        try {
            tabLayout.selectTab(tabLayout.getTabAt(1));
        } catch (Exception e) {

        }


    }

    @Override
    public void onLazyLoad() {
        System.out.println("");
        for (int i = 0; i < fragments.size(); i++) {
            Fragment fragment = fragments.get(i);
            if (fragment instanceof BaseLazyFragment) {
                ((BaseLazyFragment) fragment).onLazyLoad();
            }
        }
    }
}
